package com.softserveinc.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.Menu;
import android.support.v4.view.MenuItem;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.softserveinc.MovieManagerActivity;
import com.softserveinc.R;
import com.softserveinc.cinema.BookedSitsManager;
import com.softserveinc.cinema.CinemaSchemaView;

public class CinemaSchemaFragment extends Fragment {

	private static final String TAG = CinemaSchemaFragment.class
			.getSimpleName();

	private CinemaSchemaView cinemaView;

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setHasOptionsMenu(true);
		Log.i(TAG, "Created");
	}

	@Override
	public View onCreateView(final LayoutInflater inflater,
			final ViewGroup container, final Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.cinema_schema, container, false);

		cinemaView = (CinemaSchemaView) view
				.findViewById(R.id.cinema_schema_view);

		cinemaView.setBookedSits(BookedSitsManager.getBookedSits());

		final TextView totalPrice = (TextView) view
				.findViewById(R.id.value_total_price);
		totalPrice.setText("" + cinemaView.getTotalPrice());

		cinemaView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				totalPrice.setText("" + cinemaView.getTotalPrice());
			}
		});

		return view;
	}

	@Override
	public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
		inflater.inflate(R.menu.book_menu, menu);
	}

	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		if (item.getItemId() == R.id.menu_book_tickets) {
			BookedSitsManager.saveBookedSits(cinemaView.getBookedSits(),
					getActivity());
			MovieManagerActivity activity = (MovieManagerActivity) getActivity();

			activity.finishFragment(this);
		}
		return super.onOptionsItemSelected(item);
	}
}
